/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package avl.sv.client.source;

import avl.sv.server.training.Training;
import avl.sv.shared.Annotations.Annotation;
import avl.sv.shared.Annotations.AnnotationSet;
import avl.sv.shared.Permissions;
import avl.sv.shared.SlideReference;
import avl.sv.shared.SlideSet;
import avl.sv.shared.SlideSets;
import avl.sv.shared.XML.AnnotationXML_Writer;
import avl.sv.shared.XML.AnnotationsXML_Parser;
import avl.sv.shared.XML.SlideSetsXML_Parser;
import avl.sv.shared.sources.StudySource;
import ij.gui.MessageDialog;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;

/**
 *
 * @author benbryan
 */
public final class StudySourcePort extends StudySource {
    private final Training port;
    private long lastStudyVersion;
    public StudySourcePort(Training port,  String studyName, int id) {
        super(studyName, id);
        this.port = port;
        lastStudyVersion = getVersion();
     }
    
    public int getID(){
        return id;
    }
    
    @Override
    public boolean isStudyCurrent(){
        return lastStudyVersion == getVersion();
    }
       
    
    
    public long getVersion(){
        return port.getStudyVersion(id);
    }
    
    @Override
    public AnnotationSet getAnnotations(SlideReference slideReference) {
        try {
            String xml = port.getAnnotations(id, slideReference.slideSetName, slideReference.slideName, slideReference.hash);
            if (xml == null){
                 return new AnnotationSet(slideReference);
            }
            if (xml.contains(Permissions.PERMISSION_DENIED)){
                MessageDialog md = new MessageDialog(null, "Error", "Permission denied to view annotationd");
                return new AnnotationSet(slideReference);
            }
            AnnotationsXML_Parser parser = new AnnotationsXML_Parser();
            ArrayList<Annotation> annos = parser.parse(xml);
            AnnotationSet annoSet = new AnnotationSet(slideReference);
            for (Annotation anno:annos){
                annoSet.add(anno);
            }
            return annoSet;
        } catch (ParserConfigurationException | SAXException | IOException ex) {
            Logger.getLogger(StudySourcePort.class.getName()).log(Level.SEVERE, null, ex);
            return new AnnotationSet(slideReference);
        }
    }

    @Override
    public String setAnnotations(AnnotationSet annoSet) {
        SlideReference slideReference = annoSet.getSlideReference();
        String xml = AnnotationXML_Writer.getXMLString(annoSet.getAnnotations());
        return port.setAnnotations(id, slideReference.slideSetName, slideReference.slideName, slideReference.hash, xml);
    }
    
    @Override
    public void updateSlideSets(){
        lastStudyVersion = getVersion();
        String xml = port.getSlideSets(id);
        if (xml.equals(Permissions.PERMISSION_DENIED)){
            JOptionPane.showMessageDialog(null, Permissions.PERMISSION_DENIED);
            return;
        }
        SlideSetsXML_Parser parser = new SlideSetsXML_Parser();
        SlideSets slideSets = parser.parse(xml);
        removeAllChildren();
        for (SlideSet slideSet:slideSets.getSlideSets()){
            add(slideSet);
        }
    }
    
    @Override
    public SlideSets getSlideSets() {
        if (children == null) {
            return null;
        }
        SlideSets slideSets = new SlideSets();
        for (Object r : children) {
            if (r instanceof SlideSets) {
                slideSets.add((SlideSets) r);
            }
        }

        return slideSets;
    }

    @Override
    public void clearAnnotations(SlideReference slideReference) {
        port.clearAnnotations(id, slideReference.slideSetName, slideReference.slideName, slideReference.hash);
    }  

    @Override
    public String setDescription(String description) {
        return port.setStudyDescription(id, description);
    }

    @Override
    public String getDescription() {
        return port.getStudyDescription(id);
    }
    
    @Override
    public String delete() {
        return port.deleteStudy(id);
    }
    
}
